require("babel-register");
require("babel-polyfill");

const Koa = require('koa');
const app = new Koa();
const config =  require('./config/config').default;
const bodyParser = require('koa-bodyparser');
const static = require('koa-static');
// CORS是一个W3C标准，全称是"跨域资源共享"（Cross-origin resource sharing）。
// 下面以koa2-cors为例，1111
const cors = require('koa2-cors');


// 导入controller middleware:
const controller = require('./controller');

app.use(require('./middleware/response')());
app.use(cors({
	origin: function (ctx) {
		return "*";
		// if (ctx.url === '/test') {
		//     return "*"; // 允许来自所有域名请求
		// }
		// return 'http://119.27.163.168'; // 这样就能只允许 http://localhost:3000 这个域名的请求了
	},
	exposeHeaders: ['WWW-Authenticate', 'Server-Authorization'],
	maxAge: 5,
	credentials: true,
	allowMethods: ['GET', 'POST', 'DELETE'],
	allowHeaders: ['Content-Type', 'Authorization', 'Accept'],
}));

//配置静态资源中间件
app.use(static(__dirname + '/public'));
app.use(bodyParser());
app.use(controller());

app.listen(config.app.port, () => {
	console.log("you are listening to port 1839")
});

app.on('error', (err, ctx) => {
	log.error('server error', err, ctx)
});